package models

import (
	orm "go-papers/database"
	_ "time"
)

type OrderRefund struct {
	Id                   int     `json:"id" gorm:"type:int(11) unsigned;primary_key"`     //
	OrderId              int     `json:"orderId" gorm:"type:int(11);"`                    // 订单id
	OrderName            string  `json:"orderName" gorm:"type:varchar(255);"`             // 订单
	UserId               int     `json:"userId" gorm:"type:int(11);"`                     // 后台操作人
	UserName             string  `json:"userName" gorm:"type:varchar(255);"`              // 后台操作人
	MemberId             int     `json:"memberId" gorm:"type:int(11);"`                   // 用户id
	MemberName           string  `json:"memberName" gorm:"type:varchar(255);"`            // 用户姓名
	RefundStatus         string  `json:"refundStatus" gorm:"type:varchar(2);"`            // 申请退款状态 1 申请中/待审核  5 审核失败 / 不通过  10 审核成功 /待退款  20 已经退款/已转账
	RefundTime           string  `json:"refundTime" gorm:"type:varchar(255);"`            // 退款时间
	RefundReason         string  `json:"refundReason" gorm:"type:varchar(255);"`          // 退款原因
	RefundPrice          float64 `json:"refundPrice" gorm:"type:decimal(10,2);"`          // 退款金额
	RefundOrganPrice     float64 `json:"refundOrganPrice" gorm:"type:decimal(10,2);"`     // 机构 退款 金额
	RefundOrganMomPrice  float64 `json:"refundOrganMomPrice" gorm:"type:decimal(10,2);"`  // 机构达人 退款金额
	RefundMemberMomPrice float64 `json:"refundMemberMomPrice" gorm:"type:decimal(10,2);"` // 退社区达人 款金额
	ReturnNo             string  `json:"returnNo" gorm:"type:varchar(255);"`              // 微信分账回退 退款单号
	ReturnStatus         string  `json:"returnStatus" gorm:"type:varchar(32);"`           // 微信分账回退 退款状态
	CheckInfo            string  `json:"checkInfo" gorm:"type:varchar(255);"`             // 审核详情
	CheckTime            string  `json:"checkTime" gorm:"type:varchar(255);"`             // 审核时间

	DataScope string `json:"dataScope" gorm:"-"`
	Params    string `json:"params"  gorm:"-"`
	BaseModel
}

func (OrderRefund) TableName() string {
	return "order_refund"
}

// 创建OrderRefund
func (e *OrderRefund) Create() (OrderRefund, error) {
	var doc OrderRefund
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

//GetByID
// 申请退款状态 1 申请中/待审核
//5 审核失败 / 不通过
//10 审核成功 /待退款
//20 已经退款/已转账
func (e *OrderRefund) GetByID(oid int, mid int) (OrderRefund, error) {
	var doc OrderRefund
	table := orm.Eloquent.Table(e.TableName())
	table = table.Where("order_id = ?", oid)
	table = table.Where("member_id = ?", mid)
	table = table.Where("refund_status = ? ", 1)
	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取OrderRefund
func (e *OrderRefund) Get() (OrderRefund, error) {
	var doc OrderRefund
	table := orm.Eloquent.Table(e.TableName())
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.UserId != 0 {
		table = table.Where("user_id = ?", e.UserId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.RefundStatus != "" {
		table = table.Where("refund_status = ?", e.RefundStatus)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *OrderRefund) GetList() ([]OrderRefund, error) {
	var doc []OrderRefund

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.UserId != 0 {
		table = table.Where("user_id = ?", e.UserId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if err := table.Find(&doc).Error; err != nil {
		return nil, err
	}
	return doc, nil
}

// 获取OrderRefund带分页
func (e *OrderRefund) GetPage(pageSize int, pageIndex int) ([]OrderRefund, int, error) {
	var doc []OrderRefund

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.UserId != 0 {
		table = table.Where("user_id = ?", e.UserId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.RefundStatus != "" {
		table = table.Where("refund_status = ?", e.RefundStatus)
	}

	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新OrderRefund
func (e *OrderRefund) Update(id int) (update OrderRefund, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除OrderRefund
func (e *OrderRefund) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&OrderRefund{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *OrderRefund) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&OrderRefund{}).Error; err != nil {
		return
	}
	Result = true
	return
}
